using System;
using System.Runtime.InteropServices;

namespace UnityX
{
    internal static class RecorderVideoNativeAPI
    {
        private const string Assembly =
#if UNITY_IOS && !UNITY_EDITOR
        @"__Internal";
#else
        @"NatCorder";
#endif

        public delegate void RecordingHandler(IntPtr context, IntPtr path);

        [DllImport(Assembly, EntryPoint = @"NCCreateMP4Recorder")]
        public static extern void CreateMP4Recorder(
            [MarshalAs(UnmanagedType.LPStr)] string path,
            int width,
            int height,
            float frameRate,
            int sampleRate,
            int channelCount,
            int videoBitrate,
            int keyframeInterval,
            int audioBitRate,
            out IntPtr recorder
        );

        [DllImport(Assembly, EntryPoint = @"NCCreateHEVCRecorder")]
        public static extern void CreateHEVCRecorder(
            [MarshalAs(UnmanagedType.LPStr)] string path,
            int width,
            int height,
            float frameRate,
            int sampleRate,
            int channelCount,
            int videoBitRate,
            int keyframeInterval,
            int audioBitRate,
            out IntPtr recorder
        );

        [DllImport(Assembly, EntryPoint = @"NCCreateGIFRecorder")]
        public static extern void CreateGIFRecorder(
            [MarshalAs(UnmanagedType.LPStr)] string path,
            int width,
            int height,
            float frameDuration,
            out IntPtr recorder
        );

        [DllImport(Assembly, EntryPoint = @"NCMediaRecorderFrameSize")]
        public static extern void FrameSize(
            IntPtr recorder,
            out int width,
            out int height
        );

        [DllImport(Assembly, EntryPoint = @"NCMediaRecorderCommitFrame")]
        public static extern unsafe void CommitFrame(
            IntPtr recorder,
            void* pixelBuffer,
            long timestamp
        );

        [DllImport(Assembly, EntryPoint = @"NCMediaRecorderCommitSamples")]
        public static extern unsafe void CommitSamples(
            IntPtr recorder,
            float* sampleBuffer,
            int sampleCount,
            long timestamp
        );

        [DllImport(Assembly, EntryPoint = @"NCMediaRecorderFinishWriting")]
        public static extern void FinishWriting(
            IntPtr recorder,
            RecordingHandler handler,
            IntPtr context
        );
    }
}